package threadcoreknowledge.createthread.threadobjectclasscommommethods;

/**
 * 展示notify和notifyAll
 * 3个线程，1和2先被阻塞，由3来唤醒他们
 * start先执行不代表先启动
 */
public class WaitNotifyAll implements Runnable {

    private static final Object object = new Object();


    @Override
    public void run() {
        synchronized (object){
            System.out.println(Thread.currentThread().getName()+" got object lock");

            try {
                System.out.println(Thread.currentThread().getName() + " waits to start");
                object.wait();
                System.out.println(Thread.currentThread().getName()+" waiting to end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    public static void main(String[] args) {
        WaitNotifyAll waitNotifyAll = new WaitNotifyAll();

        Thread thread1 = new Thread(waitNotifyAll);
        Thread thread2 = new Thread(waitNotifyAll);
        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (object){
//                    object.notifyAll();
                    object.notify();
                    System.out. println(" Thread3 notified ");
                }
            }
        });

        thread1.start();
        thread2.start();
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread3.start();



    }
}
